#include <cstdlib>
#include <iostream>
#include "chess.h"
#include "time.h"

#include "Human.h"
#include "Kobold.h"

using namespace std;
/*copied from Position.h for your convenience

#define EMPTY    0
#define W_PAWN    1
#define W_KNIGHT  2
#define W_BISHOP  3
#define W_ROOK    4
#define W_QUEEN    5
#define W_KING    6
#define B_PAWN    7
#define B_KNIGHT  8
#define B_BISHOP  9
#define B_ROOK    10
#define B_QUEEN    11
#define B_KING    12
*/
int main(int argc, char** argv)
{
  srand((unsigned int)time(NULL));
  double t;
  cout << "Pray tell, how long is the time?" << endl;
  cin >> t;
  
  ChessBoard *myboard = new ChessBoard(), *nextboard = NULL;
  //default constructor sets initial board state
  Move* thismove;
  AI* ais[] = {new Kobold(t, true), new Kobold(t, true)};
  for(int i = 1; i < argc; i++)
  {
    if(strcmp(argv[i], "white") == 0)
    {
      delete ais[0];
      ais[0] = new Human();
    }
    else if(strcmp(argv[i], "black") == 0)
    {
      delete ais[1];
      ais[1] = new Human();
    }
    else if(strcmp(argv[i], "dumbwhite") == 0)
    {
      delete ais[0];
      ais[0] = new Kobold(t, false);
    }
    else if(strcmp(argv[i], "dumbblack") == 0)
    {
      delete ais[1];
      ais[1] = new Kobold(t, false);
    }
  }
  bool draw, minPlayerWin;
  myboard->printState();
  
  while (!myboard->gameOver(draw, minPlayerWin))
  {
    
    if(!( myboard->minPlayerTurn())) //white's turn
    {
      thismove = ais[0]->play();
    }
    else //black's turn
    {
      thismove = ais[1]->play();
    }
    //if a player doesn't return a move, it's a forfeit
    
    if(thismove == NULL)
    {
      minPlayerWin = !myboard->minPlayerTurn();
      break;
    }
    
    ais[0]->move(thismove);
    ais[1]->move(thismove);
    nextboard = myboard->move(thismove);
    delete myboard;
    myboard = nextboard;
    myboard->printState();
  }
  cout << endl << "Game Over!" << endl;
  if (draw)
  {
    cout << "Draw!" << endl;
    //if draw is true, it is a draw, ignore the "who won" boolean
  } else {
    if (minPlayerWin)
    {
      cout << "Black Wins!" << endl;
      //min player is black, so if minPlayerWins is true, black won
    } else {
      cout << "White Wins!" << endl;
      //if it wasn't a draw, someone had to win, so white wins because black didn't
    }
  }
  //delete unneeded for piecename, it references local literal constants
  delete myboard;
  return 0;
}
